package com.atguigu.tingshu.order.strategy.impl;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.TrackInfoFeignClient;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.order.helper.SignHelper;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.google.common.collect.Lists;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.order.strategy.OrderTradeStrategy;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 声音购买进入到结算页所对应的策略实现类
 */
@Service
@Slf4j
public class TrackOrderTradeStrategy implements OrderTradeStrategy {

    @Autowired
    private TrackInfoFeignClient trackInfoFeignClient ;

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient ;

    @Autowired
    private RedisTemplate<String , String> redisTemplate;

    @Override
    public OrderInfoVo trade(TradeVo tradeVo) {

        // 获取购买的声音的数量
        Integer trackCount = tradeVo.getTrackCount();
        if(trackCount < 0) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR) ;
        }

        // 获取付款项目的id，用户所点击的声音的id
        Long trackId = tradeVo.getItemId();

        // 根据声音的id以及trackCount查询用户要购买的声音的列表
        Result<List<TrackInfo>> paidTrackInfoList = trackInfoFeignClient.findPaidTrackInfoList(trackId, trackCount);
        List<TrackInfo> trackInfoList = paidTrackInfoList.getData();
        TrackInfo trackInfo = trackInfoList.get(0);
        Long albumId = trackInfo.getAlbumId();          // 获取专辑id
        Result<AlbumInfo> albumInfoResult = albumInfoFeignClient.getAlbumInfo(albumId);
        AlbumInfo albumInfo = albumInfoResult.getData();

        // 创建一个OrderInfoVo对象，封装结算页所需要的数据
        OrderInfoVo orderInfoVo = new OrderInfoVo() ;

        // 设置金额数据
        BigDecimal amount = albumInfo.getPrice();
        if(trackCount > 0) {
            orderInfoVo.setOriginalAmount(amount.multiply(new BigDecimal(trackCount)));
            orderInfoVo.setDerateAmount(new BigDecimal("0.00"));        // 声音购买没有折扣
            orderInfoVo.setOrderAmount(amount.multiply(new BigDecimal(trackCount)));
        }else {
            orderInfoVo.setOriginalAmount(amount);
            orderInfoVo.setDerateAmount(new BigDecimal("0.00"));        // 声音购买没有折扣
            orderInfoVo.setOrderAmount(amount);
        }

        // 设置明细相关数据
        List<OrderDetailVo> orderDetailVoList = new ArrayList<>() ;
        if(!CollectionUtils.isEmpty(trackInfoList)) {
            orderDetailVoList = trackInfoList.stream().map(track -> {
                OrderDetailVo orderDetailVo = new OrderDetailVo() ;
                orderDetailVo.setItemId(track.getId());
                orderDetailVo.setItemName(track.getTrackTitle());
                orderDetailVo.setItemUrl(track.getCoverUrl());
                orderDetailVo.setItemPrice(amount);
                return orderDetailVo ;
            }).collect(Collectors.toList()) ;
        }
        orderInfoVo.setOrderDetailVoList(orderDetailVoList);
        orderInfoVo.setOrderDerateVoList(new ArrayList<>());

        // 设置其他的数据
        this.setOtherData(orderInfoVo , tradeVo , redisTemplate);

        // 返回
        return orderInfoVo;
    }

    @Override
    public String getItemType() {
        return SystemConstant.ORDER_ITEM_TYPE_TRACK;  // 1002
    }

}
